package com.choupin.choupin.admin.controller;

import com.choupin.choupin.admin.entity.LotteryAndTurntable;
import com.choupin.choupin.admin.service.IManageTurntableService;
import com.choupin.choupin.controller.BaseController;
import com.choupin.choupin.entity.JsonResult;
import com.choupin.choupin.entity.Lottery;
import com.choupin.choupin.entity.Turntable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Map;

@CrossOrigin
@RestController
@RequestMapping("/turntable")
public class ManageTurntableController extends BaseController {

    @Autowired
    IManageTurntableService manageTurntableService;

//    显示转盘列表
    @CrossOrigin
    @PostMapping("/show")
    public JsonResult<List<LotteryAndTurntable>> showTurntables(@RequestBody Map<String,Object> data){
        JsonResult<List<LotteryAndTurntable>> jsonResult=new JsonResult<>();
        List<LotteryAndTurntable> lotteryAndTurntables=manageTurntableService.showTurntables((Integer) data.get("currentPage"),(Integer) data.get("pageSize"));
        jsonResult.setState(SUCCESS);
        jsonResult.setData(lotteryAndTurntables);
        return jsonResult;
    }

    //    联合搜索
    @CrossOrigin
    @PostMapping("/select")
    public JsonResult<List<LotteryAndTurntable>> selectTurntables(@RequestBody Map<String,Object> data ){
        JsonResult<List<LotteryAndTurntable>> jsonResult=new JsonResult<>();
        List<LotteryAndTurntable> lotteryAndTurntables=manageTurntableService.selectTurntables((String) data.get("id"),
                (String) data.get("prizeName"), (String) data.get("name"));
        jsonResult.setState(SUCCESS);
        jsonResult.setData(lotteryAndTurntables);
        return  jsonResult;
    }

//    删除转盘分片
@CrossOrigin
@PostMapping("/delete")
public JsonResult<Void> delete(@RequestBody Map<String,Object> data){
    JsonResult<Void> jsonResult=new JsonResult<>();
     manageTurntableService.deleteTurntables((Integer) data.get("id"));
    jsonResult.setState(SUCCESS);
    return jsonResult;
}

//添加转盘分片
@CrossOrigin
@PostMapping("/add")
public JsonResult<Integer> add(@RequestBody Map<String,Object> data){
    JsonResult<Integer> jsonResult=new JsonResult<>();
    Turntable turntable=new Turntable();
    turntable.setPrizeName((String) data.get("prizeName"));
    turntable.setAmount(Long.parseLong((String)data.get("amount")));
    String exchangeTime= (String) data.get("exchangeTime");
    System.out.println("取出时间："+exchangeTime);
    //        时间转换
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
    if(exchangeTime!=null&&exchangeTime!=""){
        LocalDateTime endTime=LocalDateTime.parse(exchangeTime.replace("T"," "),formatter);
        turntable.setExchangeTime(endTime);
    }
    turntable.setTurntableId(Integer.parseInt((String)data.get("turntableId")));
    turntable.setPoolId(Integer.parseInt((String)data.get("poolId")));
    Integer result= manageTurntableService.addTurntables(turntable, (String) data.get("phone"));
    jsonResult.setState(SUCCESS);
    jsonResult.setData(result);
    return  jsonResult;
}

    //    更新图片
    @CrossOrigin
    @PostMapping("/updateimage")
    public JsonResult<Void> updateImage(@RequestParam("id") Integer id  , @RequestParam("file") MultipartFile file, @RequestParam("phone") String  phone){
        JsonResult<Void> jsonResult=new JsonResult<>();
        manageTurntableService.updateImage(id,file,phone);
        jsonResult.setState(SUCCESS);
        return  jsonResult;
    }

    //    显示单个抽奖池信息数据
    @CrossOrigin
    @PostMapping("/updateshow")
    public JsonResult<Turntable> updateShow(@RequestBody Map<String,Object> data  ){
        JsonResult<Turntable> jsonResult=new JsonResult<>();
         Turntable turntable=manageTurntableService.showUpdate((Integer) data.get("id"));
        jsonResult.setState(SUCCESS);
        jsonResult.setData(turntable);
        return  jsonResult;
    }

//    更新转盘
    @CrossOrigin
    @PostMapping("/update")
    public JsonResult<Void> updateLottery(@RequestBody Map<String,Object> data  ){
        JsonResult<Void> jsonResult=new JsonResult<>();
       manageTurntableService.updateTurntable((Integer) data.get("id"),
                                              (String) data.get("prizeName"),
                                               (String) data.get("amount"),
                                               (String)data.get("exchangeTime"),
                                               (String)data.get("phone")
                                                 );
        jsonResult.setState(SUCCESS);
        return  jsonResult;
    }

}
